package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.ForumMapper;
import com.example.mapper.ForumReplyMapper;
import com.example.mapper.ForumTopicMapper;
import com.example.pojo.Forum;
import com.example.pojo.ForumReply;
import com.example.pojo.ForumTopic;
import com.example.pojo.PageBean;
import com.example.service.ForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-08-20
 */
@Service
public class ForumServiceImpl extends ServiceImpl<ForumMapper, Forum> implements ForumService {
    @Autowired
    private ForumMapper forumMapper;
    @Autowired
    private ForumTopicMapper forumTopicMapper;
    @Autowired
    private ForumReplyMapper forumReplyMapper;

    @Override
    public List<Forum> getList(Integer fid) {
        return forumMapper.getForumList(fid,null,null);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteForumWithSon(Integer fid) {
        //将其下一个排序的forum提前一位
        Forum forum = forumMapper.selectById(fid);
        LambdaQueryWrapper<Forum> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Forum::getPosition, forum.getPosition() + 1);
        Forum forum1 = forumMapper.selectOne(wrapper1);
        forum1.setPosition(forum1.getPosition() - 1);
        forumMapper.updateById(forum1);

        //删除forum
        int delForum = forumMapper.deleteById(fid);

        //删除forum的topic(首先获取tid用于删除reply)
        LambdaQueryWrapper<ForumTopic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(fid != null, ForumTopic::getForumId, fid);
        List<ForumTopic> topicList = forumTopicMapper.selectList(wrapper);
        //查询到的tid进行封装
        List<Integer> tidList = topicList.stream().map(ForumTopic::getTid).collect(Collectors.toList());
        //删除topic
        LambdaQueryWrapper<ForumTopic> delWrapper = new LambdaQueryWrapper<>();
        delWrapper.eq(fid != null, ForumTopic::getForumId, fid);
        int delTopic = forumTopicMapper.delete(delWrapper);

        //删除topic的reply(根据tid删除)
        LambdaQueryWrapper<ForumReply> delWrapperR = new LambdaQueryWrapper<>();
        delWrapperR.in(tidList.size()>0,ForumReply::getTopicId, tidList);
        int delReply = forumReplyMapper.delete(delWrapperR);

        return delForum > 0 && delReply > 0 && delTopic > 0;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean moveUp(Integer fid) {
        //  3        => 3
        //  5  当前   => 5
        //查询当前的论坛对象，
        Forum forumLocal = this.getById(fid);

        //前一个论坛下移
        Forum forumPre = forumMapper.getBeforeOne(forumLocal.getPosition());
        Integer position = forumPre.getPosition();
        forumPre.setPosition(forumLocal.getPosition());

        //当前论坛上移
        forumLocal.setPosition(position);

        boolean update1 = this.updateById(forumPre);
        boolean update = this.updateById(forumLocal);

        return update && update1;
    }

    @Override
    public boolean moveDown(Integer fid) {
        //  4 当前  +1 => 5
        //  5      -1 => 4
        //查询当前的论坛对象，position + 1
        Forum forumLocal = this.getById(fid);

        //前一个论坛下移
        Forum forumSuf = forumMapper.getAfterOne(forumLocal.getPosition());
        Integer position = forumSuf.getPosition();
        forumSuf.setPosition(forumLocal.getPosition());

        //当前论坛上移
        forumLocal.setPosition(position);

        boolean update1 = this.updateById(forumSuf);
        boolean update = this.updateById(forumLocal);

        return update && update1;
    }

    @Override
    public PageBean<Forum> getPage(Integer currentPage, Integer pageSize) {
        Integer page = (currentPage - 1) * pageSize;
        List<Forum> forumList = forumMapper.getForumList(null, page, pageSize);
        return new PageBean<>(currentPage,pageSize,count(),forumList);
    }

}
